About the project

I’m a PhD student in Genetics/proteonomics. I’m taking this course to refresh my R skills and learn some new stuff.

Introduction to Open Data Science 2018

Link to Github Repo


R Exercise 2 Analysis

Task 1

Data wrangling exersize is in the data folder.

This dataset contains information about students and their attitude/strategy towards learning and their exam scores.

setwd("~/GitHub/IODS-project/data")
data <- read.table("learning2014.csv", header = TRUE, sep = ",")
head(data)
##   Gender Age Attitude Points     Deep     Surf  Stra
## 1      F  53       37     25 3.583333 2.583333 3.375
## 2      M  55       31     12 2.916667 3.166667 2.750
## 3      F  49       25     24 3.500000 2.250000 3.625
## 4      M  53       35     10 3.500000 2.250000 3.125
## 5      M  49       37     22 3.666667 2.833333 3.625
## 6      F  38       38     21 4.750000 2.416667 3.625
names(data)
## [1] "Gender"   "Age"      "Attitude" "Points"   "Deep"     "Surf"    
## [7] "Stra"
dim(data)
## [1] 183   7

There are 7 variables and 183 people.

Task 2

library(ggplot2)
summary(data)
##  Gender       Age           Attitude         Points           Deep      
##  F:122   Min.   :17.00   Min.   :14.00   Min.   : 0.00   Min.   :1.583  
##  M: 61   1st Qu.:21.00   1st Qu.:26.00   1st Qu.:18.00   1st Qu.:3.333  
##          Median :22.00   Median :32.00   Median :22.00   Median :3.667  
##          Mean   :25.58   Mean   :31.21   Mean   :20.61   Mean   :3.696  
##          3rd Qu.:27.00   3rd Qu.:37.00   3rd Qu.:26.00   3rd Qu.:4.083  
##          Max.   :55.00   Max.   :50.00   Max.   :33.00   Max.   :4.917  
##       Surf            Stra      
##  Min.   :1.583   Min.   :1.250  
##  1st Qu.:2.417   1st Qu.:2.562  
##  Median :2.833   Median :3.125  
##  Mean   :2.792   Mean   :3.085  
##  3rd Qu.:3.167   3rd Qu.:3.625  
##  Max.   :4.333   Max.   :5.000

Summary of all variables. Dataset is about students (Age, Gender) and their attitudes towards learning (Attitude, Deep, Stra, Surf) and their combined scores (Points). Combination stats like Deep/Surf/Stra combine several questions from the questionairre togetger.

Deep is a combination of questionaire questions that reflect “Deep Approach” to learning. (Seeking Meaning, Relating Ideas, Use of Evidence)

Surf is a “Surface Approach” to learning. (Lack of Purpose, Unrelated Memorizing, Syllabus-boundness)

Stra is a “Strategic Approach” to learning. (Organized Studying, Time Management)

I will use the graphical library ggplot to do graphics.

#Pie chart of gender
ggplot(data, aes(x=factor(1), fill = factor(Gender)))+geom_bar(width=1)+coord_polar(theta="y")+theme_void()+labs(title="Gender")

There are 122 females and 61 males.

#Histogram of Age
qplot(data$Age, geom="histogram")+labs(title="Histogram for Age") +
  labs(x="Age", y="Count")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Age distribution is slanted, there are a lot more people in their 20’s than other people. Age groups cannot really be compared directly because the amount of people are so different.

#Boxplot of gender and points

ggplot(data, aes(x=Gender, y=Points, fill=Gender)) + 
  geom_boxplot()+labs(title="Gender and amount of points")

It looks like Males have slightly more points by average than females (black bar in the middle is average), but the difference might not be significant. Also there’s a lot more females (122) than males so that might affect this.

Points and Attitude:

#Linear regression of Attitude and Points
ggplot(data, aes(x = Attitude, y = Points)) + geom_point() + geom_smooth(method="lm")+labs(title="Linear regression of attitude and points")

cor.test(data$Attitude, data$Points, method="pearson")
## 
##  Pearson's product-moment correlation
## 
## data:  data$Attitude and data$Points
## t = 4.8513, df = 181, p-value = 2.635e-06
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.2042082 0.4615619
## sample estimates:
##       cor 
## 0.3392167

Test of simple linear regression. Pearson correlation is significant and low positive, 0.339. (Pearson correlation assumes normalcy, not sure if it applies here)

Scatter plot matrix:

library(GGally)
p <- ggpairs(data, mapping = aes(col=Gender, alpha=0.3), lower = list(combo = wrap("facethist", bins = 20)))
p

Compilation of all sorts of different statistics.

Task 3 & 4

Task 3

Target: Points

linear <- lm(Points ~ Gender+Age+Attitude, data=data)
summary(linear)
## 
## Call:
## lm(formula = Points ~ Gender + Age + Attitude, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -25.153  -2.520   1.716   5.411  13.022 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 10.23020    3.39239   3.016  0.00294 ** 
## GenderM     -0.39852    1.35699  -0.294  0.76934    
## Age         -0.08766    0.07944  -1.103  0.27133    
## Attitude     0.40862    0.08707   4.693 5.33e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 8.196 on 179 degrees of freedom
## Multiple R-squared:  0.1218, Adjusted R-squared:  0.1071 
## F-statistic: 8.278 on 3 and 179 DF,  p-value: 3.465e-05

This correlation is not significant for gender and age, but is for attitude. I will keep Attitude and remove Gender, Age.

summary(lm(Points~Attitude+Stra+Surf, data=data))
## 
## Call:
## lm(formula = Points ~ Attitude + Stra + Surf, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -25.151  -3.212   2.233   5.257  13.694 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  4.77806    5.15767   0.926   0.3555    
## Attitude     0.37570    0.08308   4.522 1.11e-05 ***
## Stra         1.89680    0.78318   2.422   0.0164 *  
## Surf        -0.62623    1.14945  -0.545   0.5866    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 8.082 on 179 degrees of freedom
## Multiple R-squared:  0.146,  Adjusted R-squared:  0.1317 
## F-statistic:  10.2 on 3 and 179 DF,  p-value: 3.089e-06

Linear regression is significant for Attitude and Stra, but not Surf. Remove Surf.

Task 4

summary(lm(Points~Attitude+Stra+Deep, data=data))
## 
## Call:
## lm(formula = Points ~ Attitude + Stra + Deep, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -23.499  -2.557   1.755   5.097  15.420 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  9.69651    4.84553   2.001  0.04689 *  
## Attitude     0.40408    0.08178   4.941 1.78e-06 ***
## Stra         2.07504    0.77411   2.681  0.00804 ** 
## Deep        -2.19240    1.08585  -2.019  0.04497 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 7.998 on 179 degrees of freedom
## Multiple R-squared:  0.1637, Adjusted R-squared:  0.1496 
## F-statistic: 11.68 on 3 and 179 DF,  p-value: 5.036e-07

Attitude, Strategy, and trying to get a Deep understanding of the subject during studying explains exam results the best. Here multiple linear regression and all subregressions are significant. So best predictors of a good exam score is the overall attitude of the student and study habits that aim at: Seeking Meaning, Relating Ideas, Use of Evidence, Organized Studying, and Time Management.

R-squared is a statistical measure of how close the data are to the fitted regression line. If the data formed linearly on the regression line, R-squared would be 1 or close to 1. Multiple R-squared is low and that says that these variables do not explain the whole variation in exam points. There are other factors that affect the distribution/orientation of the data.

ggplot(data, aes(x=Points, y=Attitude+Stra+Deep))+geom_point()+geom_smooth(method="lm")+labs(title="Linear regression of  points and attitude+stra+deep")

In the picture there’s slight positive linear correlation between Points and Attitude+Stra+Deep, meaning higher scores for an individual in Attitude+Stra+Deep generally mean there’s also a higher Points. While Points correlates with Attitude+Stra+Deep, those variables do not explain all variation in Points. Other factors that could affect points might be how familiar the student is with the course material previously and how much absolute time they spent on studying.

Task 5

linear <- lm(Points~Attitude+Stra+Deep, data=data)
par(mfrow = c(2,2))
plot(linear, which = c(1,2,5))

Q-Q Plot shows that the left tail of the distribution does not follow normalcy. It deviates a lot. I assume left tail contains all the “0 points” people, who answered the questions but possibly didn’t show up for the exam (0 points.) These people should be in a real analysis of the data.

There is also high spread in residuals, but they look mostly grouped linearly.

There is one higher leverage outlier in the data at 0.10 Leverage, while others are mostly between 0.0-0.04.


Chapter 3

Task 1-2

setwd("~/GitHub/IODS-project/data")
alc <- read.table("~/GitHub/IODS-project/data/alc.csv", header = TRUE, sep = ",")
names(alc)
##  [1] "school"     "sex"        "age"        "address"    "famsize"   
##  [6] "Pstatus"    "Medu"       "Fedu"       "Mjob"       "Fjob"      
## [11] "reason"     "nursery"    "internet"   "guardian"   "traveltime"
## [16] "studytime"  "failures"   "schoolsup"  "famsup"     "paid"      
## [21] "activities" "higher"     "romantic"   "famrel"     "freetime"  
## [26] "goout"      "Dalc"       "Walc"       "health"     "absences"  
## [31] "G1"         "G2"         "G3"         "alc_use"    "high_use"

This is a dataset made from two Portuguese school data attributes include descriptors of students (like age, sex, family size, profession of mom and dad, grades etc) and alcohol usage (Dalc = workday alcohol consumption, Walc = weekend alcohol consumption). Alc_use combines Dalc and Walk, and high_use is alcohol usage that’s higher than 2 (1- very low, 5- very high).

Task 3-4

Age

Legal drinking age in Portugal is 16 for beer/cider and 18 for hard liquor. I would hope that there are significantly more high alchohol users in students that are of legal drinking age.

library(ggplot2)
alc$legal <- ifelse(alc$age >= 16, TRUE, FALSE)
ggplot(data = alc, aes(x=high_use))+geom_bar()+facet_wrap("legal")+geom_text(stat='count', aes(label=..count..))

#Students under legal drinking age that are high users
18/(63+18)
## [1] 0.2222222
#Students over legal drinking age that are high users
96/(205+96)
## [1] 0.3189369

Here groupings are based on is the student legal drinking age (TRUE/FALSE). The graph shows that there are much more responders that are of legal drinking age. Percentage of students that are high users and under the legal drinking age is 22% and number of students that are high users and over the legal drinking age is 32%. There’s a noticable difference.

Go out

26 goout - going out with friends (numeric: from 1 - very low to 5 - very high)

Assumption: Kids that go out alot with their friends are more likely to drink than those who don’t.

ggplot(data = alc, aes(x=high_use))+geom_bar()+facet_wrap("goout")+geom_text(stat='count', aes(label=..count..))

ftable(table(alc$high_use, alc$goout))
##          1   2   3   4   5
##                           
## FALSE   19  84 103  41  21
## TRUE     3  16  23  40  32

Here it’s very obvious, kids that go out a lot drink a lot more than kids that don’t go out much.

Grades

Assumption: Kids who get more than half the points in every grade (First period, second period, final grade) are less likely to drink a lot than kids than don’t even manage half the points.

alc$grades <- ifelse(alc$G1 >= 11 & alc$G2 >= 11 & alc$G3 >=11, TRUE, FALSE)
ggplot(data = alc, aes(x=high_use))+geom_bar()+facet_wrap("grades")+geom_text(stat='count', aes(label=..count..))

#false
66/(66+118)
## [1] 0.3586957
#true
48/(48+150)
## [1] 0.2424242

This looks like it might be significant, kids that get more than half the points are 24% high alcohol users and kids that don’t are 35.9% high alchohol users.

Studytime

Assumption: Kids who spend more time studying drink less.

ggplot(data = alc, aes(x=high_use))+geom_bar()+facet_wrap("studytime")+geom_text(stat='count', aes(label=..count..))

ftable(table(alc$high_use, alc$studytime))
##          1   2   3   4
##                       
## FALSE   58 135  52  23
## TRUE    42  60   8   4
#1 < 2hours
42/(42+58)
## [1] 0.42
#2 2 to 5 hours
60/(60+135)
## [1] 0.3076923
#3 5 to 10 hours
8/(8+52)
## [1] 0.1333333
#4 over 10 hours
4/(4+23)
## [1] 0.1481481

Here it’s pretty clear, kids that study more than 5 hours a week are much less likely to be drinkers (14.8%, 13.3%) and kids who study less are much more likely to be heavy drinkers ( 30.7% and 42%). Especially if the kids spend almost no time studying (<2).

Task 5

Age

m <- glm(high_use ~ legal, data = alc, family = "binomial")
summary(m)
## 
## Call:
## glm(formula = high_use ~ legal, family = "binomial", data = alc)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -0.8765  -0.8765  -0.8765   1.5118   1.7344  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  -1.2528     0.2673  -4.687 2.77e-06 ***
## legalTRUE     0.4941     0.2945   1.678   0.0934 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 465.68  on 381  degrees of freedom
## Residual deviance: 462.70  on 380  degrees of freedom
## AIC: 466.7
## 
## Number of Fisher Scoring iterations: 4
coef(m)
## (Intercept)   legalTRUE 
##  -1.2527630   0.4941012
confint(m)
## Waiting for profiling to be done...
##                   2.5 %     97.5 %
## (Intercept) -1.80548222 -0.7512117
## legalTRUE   -0.06548363  1.0947388

Age is not significant factor in 95% statistical significance, but would be 90% statistical significance.

Go out

m <- glm(high_use ~ goout, data = alc, family = "binomial")
summary(m)
## 
## Call:
## glm(formula = high_use ~ goout, family = "binomial", data = alc)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.3721  -0.7672  -0.5450   0.9945   2.3082  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  -3.3512     0.4151  -8.073 6.84e-16 ***
## goout         0.7596     0.1157   6.564 5.23e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 465.68  on 381  degrees of freedom
## Residual deviance: 415.68  on 380  degrees of freedom
## AIC: 419.68
## 
## Number of Fisher Scoring iterations: 4
coef(m)
## (Intercept)       goout 
##  -3.3511801   0.7596076
confint(m)
## Waiting for profiling to be done...
##                  2.5 %     97.5 %
## (Intercept) -4.1958512 -2.5655359
## goout        0.5385448  0.9931232

How often kids go “out” with their friends is significant factor that explains high alcohol use.

Grades

m <- glm(high_use ~ grades, data = alc, family = "binomial")
summary(m)
## 
## Call:
## glm(formula = high_use ~ grades, family = "binomial", data = alc)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -0.9426  -0.9426  -0.7452   1.4320   1.6835  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  -0.5810     0.1537   -3.78 0.000157 ***
## gradesTRUE   -0.5584     0.2261   -2.47 0.013526 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 465.68  on 381  degrees of freedom
## Residual deviance: 459.51  on 380  degrees of freedom
## AIC: 463.51
## 
## Number of Fisher Scoring iterations: 4
coef(m)
## (Intercept)  gradesTRUE 
##  -0.5810299  -0.5584044
confint(m)
## Waiting for profiling to be done...
##                  2.5 %     97.5 %
## (Intercept) -0.8871701 -0.2834717
## gradesTRUE  -1.0051883 -0.1174594

Bad grades mean kid is more likely to be a high alcohol user and higher grades mean low chance.

Studytime

m <- glm(high_use ~ studytime, data = alc, family = "binomial")
summary(m)
## 
## Call:
## glm(formula = high_use ~ studytime, family = "binomial", data = alc)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.0603  -0.8314  -0.8314   1.2993   2.1010  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)   0.3209     0.3076   1.043    0.297    
## studytime    -0.6029     0.1530  -3.941  8.1e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 465.68  on 381  degrees of freedom
## Residual deviance: 448.31  on 380  degrees of freedom
## AIC: 452.31
## 
## Number of Fisher Scoring iterations: 4
coef(m)
## (Intercept)   studytime 
##   0.3209036  -0.6028561
confint(m)
## Waiting for profiling to be done...
##                  2.5 %     97.5 %
## (Intercept) -0.2772274  0.9308876
## studytime   -0.9123998 -0.3115583

If a kid spends a lot tipe studying they are less likely to be high alchohol users.

Exersize 6

Go out, Grades, STudytime

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:GGally':
## 
##     nasa
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
m <- glm(high_use ~ studytime+grades+goout, data = alc, family = "binomial")
summary(m)
## 
## Call:
## glm(formula = high_use ~ studytime + grades + goout, family = "binomial", 
##     data = alc)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.6683  -0.8015  -0.5478   0.9613   2.5753  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  -1.9907     0.5184  -3.840 0.000123 ***
## studytime    -0.5737     0.1659  -3.459 0.000543 ***
## gradesTRUE   -0.3014     0.2485  -1.213 0.225280    
## goout         0.7340     0.1171   6.269 3.64e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 465.68  on 381  degrees of freedom
## Residual deviance: 399.34  on 378  degrees of freedom
## AIC: 407.34
## 
## Number of Fisher Scoring iterations: 4
coef(m)
## (Intercept)   studytime  gradesTRUE       goout 
##  -1.9906826  -0.5736953  -0.3013601   0.7340343
confint(m)
## Waiting for profiling to be done...
##                  2.5 %     97.5 %
## (Intercept) -3.0311038 -0.9938476
## studytime   -0.9097114 -0.2575669
## gradesTRUE  -0.7897173  0.1864329
## goout        0.5103598  0.9703992
p <- predict(m, type="response")
# add the predicted probabilities to 'alc'
alc <- mutate(alc, probability = p)

# use the probabilities to make a prediction of high_use
alc <- mutate(alc, prediction = probability > 0.5)

table(high_use = alc$high_use, prediction = alc$prediction) %>% prop.table() %>% addmargins()
##         prediction
## high_use      FALSE       TRUE        Sum
##    FALSE 0.64659686 0.05497382 0.70157068
##    TRUE  0.19109948 0.10732984 0.29842932
##    Sum   0.83769634 0.16230366 1.00000000
g <- ggplot(alc, aes(x = high_use, y = probability, col=prediction))

g + geom_point()

# define a loss function (mean prediction error)
loss_func <- function(class, prob) {
  n_wrong <- abs(class - prob) > 0.5
  mean(n_wrong)
}

# call loss_func to compute the average number of wrong predictions in the (training) data
loss_func(class = alc$high_use, prob = alc$probability)
## [1] 0.2460733

There’s a 25% chance of a wrong prediction, so I don’t think my variables are a excellent predictor. It would predict “mostly”, but there’s such a big chance of error it’s not good/usable. Also there’s probably a lot of “overlap” between go out and studytime and studytime and grades… Different predictors would have maybe captured more of the actual problem?

Exersize 7

library(boot)
cv <- cv.glm(data = alc, cost = loss_func, glmfit = m, K = 10)

# average number of wrong predictions in the cross validation
cv$delta[1]
## [1] 0.2513089

Datacamp model was 0.26 and mine is 0.25… SO they are pretty much the same?

Trying to get a better model:

m <- glm(high_use ~ studytime+failures+goout+absences+sex, data = alc, family = "binomial")
summary(m)
## 
## Call:
## glm(formula = high_use ~ studytime + failures + goout + absences + 
##     sex, family = "binomial", data = alc)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -2.0668  -0.7736  -0.5080   0.7663   2.4984  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept) -3.27654    0.59805  -5.479 4.28e-08 ***
## studytime   -0.38032    0.17414  -2.184 0.028966 *  
## failures     0.24195    0.21713   1.114 0.265141    
## goout        0.71219    0.12037   5.917 3.28e-09 ***
## absences     0.07748    0.02242   3.456 0.000548 ***
## sexM         0.76991    0.26620   2.892 0.003825 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 465.68  on 381  degrees of freedom
## Residual deviance: 380.06  on 376  degrees of freedom
## AIC: 392.06
## 
## Number of Fisher Scoring iterations: 4
coef(m)
## (Intercept)   studytime    failures       goout    absences        sexM 
## -3.27653834 -0.38032241  0.24195066  0.71218501  0.07747656  0.76991496
confint(m)
## Waiting for profiling to be done...
##                   2.5 %      97.5 %
## (Intercept) -4.48345712 -2.13293535
## studytime   -0.73128369 -0.04615332
## failures    -0.18407800  0.67190587
## goout        0.48205204  0.95506124
## absences     0.03450819  0.12366371
## sexM         0.25194047  1.29791469
p <- predict(m, type="response")
# add the predicted probabilities to 'alc'
alc <- mutate(alc, probability = p)

# use the probabilities to make a prediction of high_use
alc <- mutate(alc, prediction = probability > 0.5)

table(high_use = alc$high_use, prediction = alc$prediction) %>% prop.table() %>% addmargins()
##         prediction
## high_use      FALSE       TRUE        Sum
##    FALSE 0.65968586 0.04188482 0.70157068
##    TRUE  0.17015707 0.12827225 0.29842932
##    Sum   0.82984293 0.17015707 1.00000000
g <- ggplot(alc, aes(x = high_use, y = probability, col=prediction))

g + geom_point()

# define a loss function (mean prediction error)
loss_func <- function(class, prob) {
  n_wrong <- abs(class - prob) > 0.5
  mean(n_wrong)
}

# call loss_func to compute the average number of wrong predictions in the (training) data
loss_func(class = alc$high_use, prob = alc$probability)
## [1] 0.2120419
cv <- cv.glm(data = alc, cost = loss_func, glmfit = m, K = 10)

# average number of wrong predictions in the cross validation
cv$delta[1]
## [1] 0.2251309

With studytime+failures+goout+absences+sex it drops to 22.5… So maybe that’s good enough!


Chapter 4

Task 1-3

data("Boston")
# explore the dataset
str(Boston)
## 'data.frame':    506 obs. of  14 variables:
##  $ crim   : num  0.00632 0.02731 0.02729 0.03237 0.06905 ...
##  $ zn     : num  18 0 0 0 0 0 12.5 12.5 12.5 12.5 ...
##  $ indus  : num  2.31 7.07 7.07 2.18 2.18 2.18 7.87 7.87 7.87 7.87 ...
##  $ chas   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ nox    : num  0.538 0.469 0.469 0.458 0.458 0.458 0.524 0.524 0.524 0.524 ...
##  $ rm     : num  6.58 6.42 7.18 7 7.15 ...
##  $ age    : num  65.2 78.9 61.1 45.8 54.2 58.7 66.6 96.1 100 85.9 ...
##  $ dis    : num  4.09 4.97 4.97 6.06 6.06 ...
##  $ rad    : int  1 2 2 3 3 3 5 5 5 5 ...
##  $ tax    : num  296 242 242 222 222 222 311 311 311 311 ...
##  $ ptratio: num  15.3 17.8 17.8 18.7 18.7 18.7 15.2 15.2 15.2 15.2 ...
##  $ black  : num  397 397 393 395 397 ...
##  $ lstat  : num  4.98 9.14 4.03 2.94 5.33 ...
##  $ medv   : num  24 21.6 34.7 33.4 36.2 28.7 22.9 27.1 16.5 18.9 ...
dim(Boston)
## [1] 506  14
summary(Boston)
##       crim                zn             indus            chas        
##  Min.   : 0.00632   Min.   :  0.00   Min.   : 0.46   Min.   :0.00000  
##  1st Qu.: 0.08204   1st Qu.:  0.00   1st Qu.: 5.19   1st Qu.:0.00000  
##  Median : 0.25651   Median :  0.00   Median : 9.69   Median :0.00000  
##  Mean   : 3.61352   Mean   : 11.36   Mean   :11.14   Mean   :0.06917  
##  3rd Qu.: 3.67708   3rd Qu.: 12.50   3rd Qu.:18.10   3rd Qu.:0.00000  
##  Max.   :88.97620   Max.   :100.00   Max.   :27.74   Max.   :1.00000  
##       nox               rm             age              dis        
##  Min.   :0.3850   Min.   :3.561   Min.   :  2.90   Min.   : 1.130  
##  1st Qu.:0.4490   1st Qu.:5.886   1st Qu.: 45.02   1st Qu.: 2.100  
##  Median :0.5380   Median :6.208   Median : 77.50   Median : 3.207  
##  Mean   :0.5547   Mean   :6.285   Mean   : 68.57   Mean   : 3.795  
##  3rd Qu.:0.6240   3rd Qu.:6.623   3rd Qu.: 94.08   3rd Qu.: 5.188  
##  Max.   :0.8710   Max.   :8.780   Max.   :100.00   Max.   :12.127  
##       rad              tax           ptratio          black       
##  Min.   : 1.000   Min.   :187.0   Min.   :12.60   Min.   :  0.32  
##  1st Qu.: 4.000   1st Qu.:279.0   1st Qu.:17.40   1st Qu.:375.38  
##  Median : 5.000   Median :330.0   Median :19.05   Median :391.44  
##  Mean   : 9.549   Mean   :408.2   Mean   :18.46   Mean   :356.67  
##  3rd Qu.:24.000   3rd Qu.:666.0   3rd Qu.:20.20   3rd Qu.:396.23  
##  Max.   :24.000   Max.   :711.0   Max.   :22.00   Max.   :396.90  
##      lstat            medv      
##  Min.   : 1.73   Min.   : 5.00  
##  1st Qu.: 6.95   1st Qu.:17.02  
##  Median :11.36   Median :21.20  
##  Mean   :12.65   Mean   :22.53  
##  3rd Qu.:16.95   3rd Qu.:25.00  
##  Max.   :37.97   Max.   :50.00
# calculate the correlation matrix and round it
cor_matrix<-cor(Boston) %>% round(digits = 2)

# print the correlation matrix
cor_matrix
##          crim    zn indus  chas   nox    rm   age   dis   rad   tax
## crim     1.00 -0.20  0.41 -0.06  0.42 -0.22  0.35 -0.38  0.63  0.58
## zn      -0.20  1.00 -0.53 -0.04 -0.52  0.31 -0.57  0.66 -0.31 -0.31
## indus    0.41 -0.53  1.00  0.06  0.76 -0.39  0.64 -0.71  0.60  0.72
## chas    -0.06 -0.04  0.06  1.00  0.09  0.09  0.09 -0.10 -0.01 -0.04
## nox      0.42 -0.52  0.76  0.09  1.00 -0.30  0.73 -0.77  0.61  0.67
## rm      -0.22  0.31 -0.39  0.09 -0.30  1.00 -0.24  0.21 -0.21 -0.29
## age      0.35 -0.57  0.64  0.09  0.73 -0.24  1.00 -0.75  0.46  0.51
## dis     -0.38  0.66 -0.71 -0.10 -0.77  0.21 -0.75  1.00 -0.49 -0.53
## rad      0.63 -0.31  0.60 -0.01  0.61 -0.21  0.46 -0.49  1.00  0.91
## tax      0.58 -0.31  0.72 -0.04  0.67 -0.29  0.51 -0.53  0.91  1.00
## ptratio  0.29 -0.39  0.38 -0.12  0.19 -0.36  0.26 -0.23  0.46  0.46
## black   -0.39  0.18 -0.36  0.05 -0.38  0.13 -0.27  0.29 -0.44 -0.44
## lstat    0.46 -0.41  0.60 -0.05  0.59 -0.61  0.60 -0.50  0.49  0.54
## medv    -0.39  0.36 -0.48  0.18 -0.43  0.70 -0.38  0.25 -0.38 -0.47
##         ptratio black lstat  medv
## crim       0.29 -0.39  0.46 -0.39
## zn        -0.39  0.18 -0.41  0.36
## indus      0.38 -0.36  0.60 -0.48
## chas      -0.12  0.05 -0.05  0.18
## nox        0.19 -0.38  0.59 -0.43
## rm        -0.36  0.13 -0.61  0.70
## age        0.26 -0.27  0.60 -0.38
## dis       -0.23  0.29 -0.50  0.25
## rad        0.46 -0.44  0.49 -0.38
## tax        0.46 -0.44  0.54 -0.47
## ptratio    1.00 -0.18  0.37 -0.51
## black     -0.18  1.00 -0.37  0.33
## lstat      0.37 -0.37  1.00 -0.74
## medv      -0.51  0.33 -0.74  1.00
# visualize the correlation matrix
corrplot(cor_matrix, method="circle", type="upper", cl.pos="b", tl.pos="d", tl.cex = 0.6)

Boston Dataset describes Housing Values in Suburbs of Boston. Here positive correlations are displayed in blue and negative correlations in red color. Color intensity and the size of the circle are proportional to the correlation coefficients.

f.ex. Age is strongly negatively correlated with weighted mean of distances to five Boston employment centres (dis). And index of accessibility to radial highways (rad) is strongly positively correlated with property tax. Number of rooms is positively corrrelated with median value of homes, whereas lower status of the population is strongly negatively correlated.

Task 4

# center and standardize variables
boston_scaled <- scale(Boston)

# summaries of the scaled variables
summary(boston_scaled)
##       crim                 zn               indus        
##  Min.   :-0.419367   Min.   :-0.48724   Min.   :-1.5563  
##  1st Qu.:-0.410563   1st Qu.:-0.48724   1st Qu.:-0.8668  
##  Median :-0.390280   Median :-0.48724   Median :-0.2109  
##  Mean   : 0.000000   Mean   : 0.00000   Mean   : 0.0000  
##  3rd Qu.: 0.007389   3rd Qu.: 0.04872   3rd Qu.: 1.0150  
##  Max.   : 9.924110   Max.   : 3.80047   Max.   : 2.4202  
##       chas              nox                rm               age         
##  Min.   :-0.2723   Min.   :-1.4644   Min.   :-3.8764   Min.   :-2.3331  
##  1st Qu.:-0.2723   1st Qu.:-0.9121   1st Qu.:-0.5681   1st Qu.:-0.8366  
##  Median :-0.2723   Median :-0.1441   Median :-0.1084   Median : 0.3171  
##  Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000  
##  3rd Qu.:-0.2723   3rd Qu.: 0.5981   3rd Qu.: 0.4823   3rd Qu.: 0.9059  
##  Max.   : 3.6648   Max.   : 2.7296   Max.   : 3.5515   Max.   : 1.1164  
##       dis               rad               tax             ptratio       
##  Min.   :-1.2658   Min.   :-0.9819   Min.   :-1.3127   Min.   :-2.7047  
##  1st Qu.:-0.8049   1st Qu.:-0.6373   1st Qu.:-0.7668   1st Qu.:-0.4876  
##  Median :-0.2790   Median :-0.5225   Median :-0.4642   Median : 0.2746  
##  Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000  
##  3rd Qu.: 0.6617   3rd Qu.: 1.6596   3rd Qu.: 1.5294   3rd Qu.: 0.8058  
##  Max.   : 3.9566   Max.   : 1.6596   Max.   : 1.7964   Max.   : 1.6372  
##      black             lstat              medv        
##  Min.   :-3.9033   Min.   :-1.5296   Min.   :-1.9063  
##  1st Qu.: 0.2049   1st Qu.:-0.7986   1st Qu.:-0.5989  
##  Median : 0.3808   Median :-0.1811   Median :-0.1449  
##  Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000  
##  3rd Qu.: 0.4332   3rd Qu.: 0.6024   3rd Qu.: 0.2683  
##  Max.   : 0.4406   Max.   : 3.5453   Max.   : 2.9865
# class of the boston_scaled object
class(boston_scaled)
## [1] "matrix"
# change the object to data frame
boston_scaled <- as.data.frame(boston_scaled)

# summary of the scaled crime rate
summary(boston_scaled$crim)
##      Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
## -0.419367 -0.410563 -0.390280  0.000000  0.007389  9.924110
# create a quantile vector of crim and print it
bins <- quantile(boston_scaled$crim)
bins
##           0%          25%          50%          75%         100% 
## -0.419366929 -0.410563278 -0.390280295  0.007389247  9.924109610
# create a categorical variable 'crime'
crime <- cut(boston_scaled$crim, breaks = bins, include.lowest = TRUE, labels = c("low", "med_low", "med_high", "high"))

# look at the table of the new factor crime
table(crime)
## crime
##      low  med_low med_high     high 
##      127      126      126      127
# remove original crim from the dataset
boston_scaled <- dplyr::select(boston_scaled, -crim)

# add the new categorical value to scaled data
boston_scaled <- data.frame(boston_scaled, crime)

# number of rows in the Boston dataset 
n <- nrow(boston_scaled)

# choose randomly 80% of the rows
ind <- sample(n,  size = n * 0.8)

# create train set
train <- boston_scaled[ind,]

# create test set 
test <- boston_scaled[-ind,]

# save the correct classes from test data
correct_classes <- test$crime

# remove the crime variable from test data
test <- dplyr::select(test, -crime)

In the scaling we subtract the column means from the corresponding columns and divide the difference with standard deviation so the values become z-scores. This normalizes the data so now it will be normally distributed. For some multivariate techniques such as multidimensional scaling and cluster analysis, the concept of distance between the units in the data is often of considerable interest and importance. When the variables in a multivariate data set are on different scales, it makes more sense to calculate the distances after some form of standardization.

Task 5

# linear discriminant analysis
lda.fit <- lda(crime ~ ., data = train)

# print the lda.fit object
lda.fit
## Call:
## lda(crime ~ ., data = train)
## 
## Prior probabilities of groups:
##      low  med_low med_high     high 
## 0.259901 0.250000 0.240099 0.250000 
## 
## Group means:
##                   zn      indus          chas        nox          rm
## low       1.04674041 -0.9249028 -0.1223442968 -0.8740729  0.44740737
## med_low  -0.09731296 -0.3236341  0.0005392655 -0.6043588 -0.07470053
## med_high -0.37054365  0.2047360  0.0929688923  0.4047620  0.18904966
## high     -0.48724019  1.0171306 -0.0774231154  1.0294057 -0.40041345
##                 age        dis        rad        tax     ptratio
## low      -0.9010817  0.9163203 -0.6821760 -0.7256223 -0.41497152
## med_low  -0.4063844  0.3886193 -0.5509118 -0.5179664 -0.05309119
## med_high  0.4191027 -0.3916045 -0.4052697 -0.3030324 -0.38469937
## high      0.8030977 -0.8496991  1.6379981  1.5139626  0.78062517
##                black       lstat        medv
## low       0.38147837 -0.77250305  0.53977170
## med_low   0.31941250 -0.19990287  0.03680298
## med_high  0.09061597 -0.01694445  0.24561495
## high     -0.77839577  0.91309970 -0.68921118
## 
## Coefficients of linear discriminants:
##                 LD1          LD2         LD3
## zn       0.08069079  0.735376091 -0.94059732
## indus    0.03225585 -0.342607958  0.28000290
## chas    -0.08537396 -0.004390383  0.16962190
## nox      0.38737717 -0.677037621 -1.32248675
## rm      -0.08837978 -0.142888669 -0.09900877
## age      0.22178887 -0.297763827 -0.15052175
## dis     -0.06380347 -0.330256124  0.15823936
## rad      3.09834362  0.953981811  0.06783711
## tax      0.08818910 -0.075265917  0.32859053
## ptratio  0.10964107  0.099239020 -0.19897887
## black   -0.12833687 -0.003542823  0.08394312
## lstat    0.25786935 -0.249971124  0.30800226
## medv     0.21743981 -0.392302137 -0.23726883
## 
## Proportion of trace:
##    LD1    LD2    LD3 
## 0.9467 0.0394 0.0139
# the function for lda biplot arrows
lda.arrows <- function(x, myscale = 1, arrow_heads = 0.1, color = "orange", tex = 0.75, choices = c(1,2)){
  heads <- coef(x)
  arrows(x0 = 0, y0 = 0, 
         x1 = myscale * heads[,choices[1]], 
         y1 = myscale * heads[,choices[2]], col=color, length = arrow_heads)
  text(myscale * heads[,choices], labels = row.names(heads), 
       cex = tex, col=color, pos=3)
}

# target classes as numeric
classes <- as.numeric(train$crime)

# plot the lda results
plot(lda.fit, dimen = 2, col = classes, pch = classes)
lda.arrows(lda.fit, myscale = 1)

Here Linear Discriminant 1 explains most of the between group variance.

rad: index of accessibility to radial highways is the most significant factor that predicts higher crime rate.

Task 6

# predict classes with test data
lda.pred <- predict(lda.fit, newdata = test)

# cross tabulate the results
table(correct = correct_classes, predicted = lda.pred$class)
##           predicted
## correct    low med_low med_high high
##   low       11      11        0    0
##   med_low    3      16        6    0
##   med_high   0      15       13    1
##   high       0       0        0   26

This prediction is very good at predicting high crime rates, but worse at predicting med_low or med_high correctly. ’ ##Task 7

data("Boston")
new_boston <- scale(Boston)
# k-means clustering
km <-kmeans(new_boston, centers = 4)

# plot the Boston dataset with clusters
pairs(new_boston, col = km$cluster)

#K-means might produce different results every time, because it randomly assigns the initial cluster centers. The function set.seed() can be used to deal with that.
set.seed(123)

# determine the number of clusters
k_max <- 10

# calculate the total within sum of squares
twcss <- sapply(1:k_max, function(k){kmeans(new_boston, k)$tot.withinss})

# visualize the results
qplot(x = 1:k_max, y = twcss, geom = 'line')

# k-means clustering
km <-kmeans(new_boston, centers = 2)

# plot the Boston dataset with clusters
pairs(new_boston, col = km$cluster)

One way to check optimal amount of clusters is to look at the total within cluster sum of squares (twcss). When twcss drops a lot the optimal number of clusters is found. Here twcss drops around 2. In the LDA there were two “main clusters” one with high crime and some points from med_high and the other larger cluster was the rest of the data points. This k-means clustering on this data also works best with 2 clusters.

Bonuses

km <-kmeans(new_boston, centers = 3)

# linear discriminant analysis
lda.fit <- lda(crime ~ ., data = train)

# print the lda.fit object
lda.fit
## Call:
## lda(crime ~ ., data = train)
## 
## Prior probabilities of groups:
##      low  med_low med_high     high 
## 0.259901 0.250000 0.240099 0.250000 
## 
## Group means:
##                   zn      indus          chas        nox          rm
## low       1.04674041 -0.9249028 -0.1223442968 -0.8740729  0.44740737
## med_low  -0.09731296 -0.3236341  0.0005392655 -0.6043588 -0.07470053
## med_high -0.37054365  0.2047360  0.0929688923  0.4047620  0.18904966
## high     -0.48724019  1.0171306 -0.0774231154  1.0294057 -0.40041345
##                 age        dis        rad        tax     ptratio
## low      -0.9010817  0.9163203 -0.6821760 -0.7256223 -0.41497152
## med_low  -0.4063844  0.3886193 -0.5509118 -0.5179664 -0.05309119
## med_high  0.4191027 -0.3916045 -0.4052697 -0.3030324 -0.38469937
## high      0.8030977 -0.8496991  1.6379981  1.5139626  0.78062517
##                black       lstat        medv
## low       0.38147837 -0.77250305  0.53977170
## med_low   0.31941250 -0.19990287  0.03680298
## med_high  0.09061597 -0.01694445  0.24561495
## high     -0.77839577  0.91309970 -0.68921118
## 
## Coefficients of linear discriminants:
##                 LD1          LD2         LD3
## zn       0.08069079  0.735376091 -0.94059732
## indus    0.03225585 -0.342607958  0.28000290
## chas    -0.08537396 -0.004390383  0.16962190
## nox      0.38737717 -0.677037621 -1.32248675
## rm      -0.08837978 -0.142888669 -0.09900877
## age      0.22178887 -0.297763827 -0.15052175
## dis     -0.06380347 -0.330256124  0.15823936
## rad      3.09834362  0.953981811  0.06783711
## tax      0.08818910 -0.075265917  0.32859053
## ptratio  0.10964107  0.099239020 -0.19897887
## black   -0.12833687 -0.003542823  0.08394312
## lstat    0.25786935 -0.249971124  0.30800226
## medv     0.21743981 -0.392302137 -0.23726883
## 
## Proportion of trace:
##    LD1    LD2    LD3 
## 0.9467 0.0394 0.0139
# the function for lda biplot arrows
lda.arrows <- function(x, myscale = 1, arrow_heads = 0.1, color = "orange", tex = 0.75, choices = c(1,2)){
  heads <- coef(x)
  arrows(x0 = 0, y0 = 0, 
         x1 = myscale * heads[,choices[1]], 
         y1 = myscale * heads[,choices[2]], col=color, length = arrow_heads)
  text(myscale * heads[,choices], labels = row.names(heads), 
       cex = tex, col=color, pos=3)
}

# target classes as numeric
classes <- as.numeric(km$cluster)

# plot the lda results
plot(lda.fit, dimen = 2, col = classes, pch = classes)
lda.arrows(lda.fit, myscale = 1)

rad is the most influencial linear separator for the clusters. It looks like the k-means clustering with 4 clusters makes clusters where low, med_low, med_high, high etc. mix a lot so the clustering is not perfect.

library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:MASS':
## 
##     select
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
model_predictors <- dplyr::select(train, -crime)

# check the dimensions
dim(model_predictors)
## [1] 404  13
dim(lda.fit$scaling)
## [1] 13  3
# matrix multiplication
matrix_product <- as.matrix(model_predictors) %*% lda.fit$scaling
matrix_product <- as.data.frame(matrix_product)

plot_ly(x = matrix_product$LD1, y = matrix_product$LD2, z = matrix_product$LD3, type= 'scatter3d', mode='markers', color= ~train$crime)
#plot_ly(x = matrix_product$LD1, y = matrix_product$LD2, z = matrix_product$LD3, type= 'scatter3d', mode='markers', color=~classes)

Can’t get the color with km$cluster to work…